home *** CD-ROM | disk | FTP | other *** search
- /* TTPictures.c
- * Display TeachText style pictures in Writeswell Jr.
- * ©1993 Working Software, Inc.
- * This source code is copyrighted. Permission is granted to use the Word Services
- * portion of the Writeswell Jr. source code in your own programs, but you
- * may not distribute the Writeswell Jr. word-processor code as a
- * commercial product. If you modify the code, please do not call it
- * Writeswell Jr. (or Writeswell.) This will ensure that people understand the
- * program and don’t have to deal with a number of different versions with
- * who-knows-what going on in the code.
- *
- * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
- * 25 Feb 93 Mike Crawford
- */
-
- #include "AppleEvents.h"
- #include "TBConstants.h"
- #include "TBGlobals.h"
- #include "TestBed.h"
- #include "TTPictures.h"
- #include "MyTextEdit.h"
-
- void ShowDocPictures( TEHandle textH, short numPictures );
- void DrawAllPictures( TEHandle textH, short numPictures );
- void DrawOnePicture( PicHandle picH, TEHandle textH, short charNum );
- short GetCharLine( TEHandle textH, short charNum );
-
- void ShowPictures( WindowPtr docWindow, short numPictures, short resRefNum )
- {
- short curFile;
- TEHandle textH;
- GrafPtr curPort;
-
- if ( !numPictures )
- return;
-
- if ( resRefNum == -1 )
- return;
-
- curFile = CurResFile();
- UseResFile( resRefNum );
-
- GetPort( &curPort );
- SetPort( docWindow );
-
- textH = GetTextHandle( docWindow );
-
- ShowDocPictures( textH, numPictures );
-
- SetPort( curPort );
-
- UseResFile( curFile );
-
- return;
- }
-
- void ShowDocPictures( TEHandle textH, short numPictures )
- {
- RgnHandle oldClip;
- RgnHandle newClip;
- RgnHandle viewRgn;
- Rect viewRect;
-
- /* Set the clip region to the intersection of the existing clip region
- * and view rectangle (so that we don't draw over the scroll bar).
- * Then draw all the pictures.
- *
- * We put the loop to do all the drawing in a separate function so that
- * we only need one instance of the code where we restore the clip region.
- */
-
- viewRect = (*textH)->viewRect;
-
- oldClip = NewRgn();
- if (!oldClip )
- return;
-
- newClip = NewRgn();
- if (!newClip ){
- DisposeRgn( oldClip );
- return;
- }
-
- viewRgn = NewRgn();
- if (!viewRgn ){
- DisposeRgn( oldClip );
- DisposeRgn( newClip );
- return;
- }
-
- GetClip( oldClip ); /* Save the original clip region */
-
- GetClip( newClip ); /* Get the original clip region to work on */
-
- RectRgn( viewRgn, &viewRect );
-
- SectRgn( newClip, viewRgn, newClip ); /* Get the intersection with the viewRect */
-
- SetClip( newClip );
-
- DisposeRgn( newClip ); /* Toss now-unneeded region handles */
- DisposeRgn( viewRgn );
-
- DrawAllPictures( textH, numPictures );
-
- SetClip( oldClip ); /* Restore the original clip region */
-
- DisposeRgn( oldClip );
-
- return;
- }
-
- void DrawAllPictures( TEHandle textH, short numPictures )
- {
- CharsHandle charH;
- unsigned char *charP;
- short charNum;
- short picNum;
- short textLen;
- short lastChar;
- short bottomChar;
- PicHandle picH;
- Rect viewRect;
- Point bottomPoint;
-
- charH = TEGetText( textH );
-
- textLen = (*textH)->teLength;
-
- viewRect = (*textH)->viewRect;
-
- bottomPoint.h = viewRect.right;
- bottomPoint.v = viewRect.bottom;
-
- bottomChar = TEGetOffset( bottomPoint, textH );
-
- if ( bottomChar < textLen )
- lastChar = bottomChar;
- else
- lastChar = textLen;
-
- charNum = 0;
-
- picNum = 0;
-
- charP = (unsigned char*)*charH; /* Warning: deref unlock handle */
-
- for ( charNum = 0; charNum < lastChar; charNum++ ){
-
- /* Display a picture at each non-breaking space */
-
- if ( *charP++ == 0xca ){
-
- picH = (PicHandle)Get1Resource( 'PICT', 1000 + picNum );
- if ( !picH ){
- return;
- }
-
- picNum++;
-
- DrawOnePicture( picH, textH, charNum );
-
- if ( picNum >= numPictures ){
- return;
- }
-
- /* Refresh the character pointer, since its handle may have moved */
-
- charP = (unsigned char*)*charH + charNum + 1;
- }
- }
-
- return;
- }
-
- void DrawOnePicture( PicHandle picH, TEHandle textH, short charNum )
- {
- Rect dispRect;
- short picHeight;
- short picWidth;
- Rect viewRect;
- short viewWidth;
- Point charPoint;
-
- charPoint = TEGetPoint( charNum, textH );
-
- dispRect = (*picH)->picFrame;
-
- picHeight = dispRect.bottom - dispRect.top;
-
- dispRect.top = charPoint.v;
-
- dispRect.bottom = dispRect.top + picHeight;
-
- picWidth = dispRect.right - dispRect.left;
-
- viewRect = (*textH)->viewRect;
-
- viewWidth = viewRect.right - viewRect.left;
-
- dispRect.left = viewRect.left + ( ( viewWidth - picWidth ) / 2 ); /* 1.1.1 MDC Center picture */
-
- dispRect.right = dispRect.left + picWidth;
-
- DrawPicture( picH, &dispRect );
-
- return;
- }
-
- void ScrollPictures( short dV, TEHandle textH )
- {
- RgnHandle oldClip;
- Rect scrollClipRect;
-
- /* 2.0 Display the pictures.
- * A side effect of TEScroll is that it will scroll the picture as well - it
- * actually uses ScrollBits. So we only need to redisplay the part of the
- * picture that has just come into view.
- */
-
-
- oldClip = NewRgn();
-
- if ( !oldClip )
- return;
-
- GetClip( oldClip );
-
- scrollClipRect = (*textH)->viewRect;
-
- if ( dV > 0 ){
-
- /* We are scrolling downward - increasing y values are lower */
-
- scrollClipRect.bottom = scrollClipRect.top + dV;
-
- }else{
-
- /* We are scrolling upward */
-
- scrollClipRect.top = scrollClipRect.bottom + dV; /* Plus 'cause dV is negative */
- }
-
- ClipRect( &scrollClipRect );
-
- ShowPictures( gScrollWindow, gNumPictures, gResRefNum );
-
- SetClip( oldClip );
-
- DisposeRgn( oldClip );
-
- return;
- }
- #ifdef NEVER
- void DrawOnePicture( PicHandle picH, TEHandle textH, short charNum )
- {
- Rect dispRect;
- Rect viewRect;
- Rect destRect;
- short charLine;
- short picHeight;
- short picWidth;
- short charHeight;
-
- charLine = GetCharLine( textH, charNum );
-
- charHeight = TEGetHeight( charLine, 1, textH );
-
- dispRect = (*picH)->picFrame;
-
- picHeight = dispRect.bottom - dispRect.top;
-
- viewRect = (*textH)->viewRect;
- destRect = (*textH)->destRect;
-
- dispRect.top = destRect.top - viewRect.top + charHeight;
- dispRect.bottom = dispRect.top + picHeight;
-
- picWidth = dispRect.right - dispRect.left;
-
- dispRect.left = kTextInset; /* So picture is not right on edge of window */
-
- dispRect.right = picWidth + kTextInset;
-
- DrawPicture( picH, &dispRect );
-
- return;
- }
-
- short GetCharLine( TEHandle textH, short charNum )
- {
- short lineNum;
- short *linePtr;
- short nLines;
-
- linePtr = (*textH)->lineStarts; /* Warning: Handle deref */
- nLines = (*textH)->nLines;
-
- for ( lineNum = 0; lineNum < nLines; lineNum++ ){
-
- if ( *linePtr++ > charNum ){
- return lineNum + 1; /* TE uses 1 as the first line */
- }
- }
-
- return lineNum;
- }
- #endif
-